﻿#include "tcpclient.h"
#include "ui_tcpclient.h"
#include"login.h"
#include"serverfile.h"
#include"clientfile.h"
#include "odbclib.h"
#include <commonhelper.h>
#include <QDateTime>
#include <QHostAddress>
#include <QMessageBox>
#include <qmessagebox.h>
#include <userinf.h>
#include <facer.h>

tcpclient::tcpclient(login *l,QWidget *parent) :
    QWidget(parent),
    l(l),
    ui(new Ui::tcpclient)
{
    ui->setupUi(this);
    setWindowTitle(QString::fromLocal8Bit("聊天室"));

    //设置图标图片
    this->setWindowIcon(QIcon(":/photo/11.jfif"));

    //设置皮肤
    setfacer(4);


    //存入用户名字
    QString login_name=l->getid86();
    ui->name->setText(login_name);

    //显示时间
    QTimer *timer_calendar= new QTimer(this);;//new一个QTimer对象
    connect(timer_calendar,SIGNAL(timeout()),this,SLOT(showtime()));//连接槽函数
    timer_calendar->start(1000);//每一秒溢出一次进入槽函数



    qDebug()<<userName;
    //将进入服务器的标志位置为false
    status = false;
    port = 8888;
    //初始化serverIP为空地址
    serverIP = new QHostAddress();
}

void tcpclient::set_userName(QString s1)
{
    userName=s1;
}

void tcpclient::connect_client()
{
    //首先判断这个用户是不是在服务器中
     if(status == false) {  //不在服务器中就和服务器进行连接
         if(!serverIP->setAddress(ip)) {   //判断IP地址是否可以被正确解析
             QMessageBox::warning(this, " 错误 ", " IP地址不正确 ");
             return;
         }
         tcpsocket = new QTcpSocket(this);
         tcpsocket->connectToHost(*serverIP, port);

         //和服务器连接成功能会触发connected信号
         connect(tcpsocket, &QTcpSocket::connected, this, &tcpclient::slotconnectedsuccess);


         //接收到服务器的信息就会触发readyRead信号
         connect(tcpsocket, &QTcpSocket::readyRead, this, &tcpclient::slotreceive);

         //将进入服务器的标志位置为true；
         status = true;
     }
     else {   //已经进入了服务器
         tcpsocket->disconnectFromHost();
         status = false;
     }
}


tcpclient::~tcpclient()
{
    delete ui;
}

void tcpclient::slotconnectedsuccess()
{

    ui->readtext->append(QString::fromLocal8Bit("连接成功"));

    //进入服务器可以发送信息了
    ui->sendButton->setEnabled(true);

    //将时间字符化然后发送
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_date =current_date_time.toString("yyyy.MM.dd hh:mm:ss \n");
    QString msg2=current_date;
    tcpsocket->write(msg2.toUtf8().data());

    QString msg = userName + QString::fromLocal8Bit("：登录账号");
    tcpsocket->write(msg.toUtf8().data());

}

//用readall（）读取服务器发来的数据，然后发送到readtext上
void tcpclient::slotreceive()
{
    QByteArray array = tcpsocket->readAll();

    ui->readtext->append(array.data());
}


void tcpclient::on_sendButton_clicked()
{

    if(ui->writetext->toPlainText() == "") {
        return;
    }

    //将时间字符化然后发送
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_date =current_date_time.toString("yyyy.MM.dd hh:mm:ss \n");
    QString msg2=current_date;
    tcpsocket->write(msg2.toUtf8().data());

    //获取编辑区内容
    QString str = ui->writetext->toPlainText();
    QString strHead = QString("%1%3").arg(userName + ": ").arg(str);
    tcpsocket->write(strHead.toUtf8().data());
    ui->writetext->clear();

}


void tcpclient::on_connectButton_clicked()
{

    //将时间字符化然后发送
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_date =current_date_time.toString("yyyy.MM.dd hh:mm:ss \n");
    QString msg2=current_date;
    tcpsocket->write(msg2.toUtf8().data());


    QString msg = userName + QString::fromLocal8Bit(":下线");
    tcpsocket->write(msg.toUtf8().data());
    tcpsocket->disconnectFromHost();
    status = false;
    //关闭窗口
    close();
}


void tcpclient::on_comboBox_activated(int index)
{
    if(index==0){
        //账号信息  连接数据库查询
        ODBClib *o=new ODBClib;
       QSqlDatabase db=o->connectdb();
         QSqlQuery qsQuery=QSqlQuery(db);

        QString str=QString("select * from myqicq_user where id='%1'").arg(userName);
        qsQuery.exec(str);
        qsQuery.first();
        //查询到每一个数据封装 到userinf中的成员中
       QString id= qsQuery.value(0).toString();
       QString realname= qsQuery.value(2).toString();
       int sex= qsQuery.value(3).toInt();
       QString typewrite= qsQuery.value(4).toString();

       userinf *user1 =new userinf(sex,realname,id,typewrite);
       user1->setid(userName);
       user1->setname(realname);
       user1->setsex(sex);
       user1->settypewrite(typewrite);
        user1->show();
    }
    else  if(index==1){
        //发送文件到服务器
        clientfile *c1=new clientfile;
        Serverfile *s1=new Serverfile;
        c1->show();
        s1->show();
    }
    else  if(index==2){
        //换肤
        facer *f=new facer(this,l);
        f->show();

    }
    else  if(index==3){
            //更换账户
        login *l=new login;
        close();
        l->show();
    }

}

void tcpclient::showtime()
{
        QDateTime time = QDateTime::currentDateTime();
        QString str = time.toString("yyyy-MM-dd hh:mm:ss");
        ui->date->setText(str);

        //设置时间颜色
        QPalette pa;
        pa.setColor(QPalette::WindowText,Qt::red);
        ui->date->setPalette(pa);
}


//更改皮肤背景
void tcpclient::setfacer(int index)
{

    //换背景
    QImage _image;   //创建image
   //利用字符串拼接 完成photo
    QString str=QString(":/photo/back%1.jpg").arg(index);
   _image.load(str);
   setAutoFillBackground(true);   // 这个属性一定要设置
    QPalette pal(palette());
     pal.setBrush(QPalette::Window, QBrush(_image.scaled(size(), Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
    setPalette(pal);
}

